import java.util.*;

public class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = arrToSet(nums1);
        Set<Integer> set2 = arrToSet(nums2);
        set1.retainAll(set2);

        int[] ints = new int[set1.size()];
        int i=0;
        for(Integer anInt :set1){
            ints[i]=anInt;
            i++;
        }
        return ints;
    }

    public Set<Integer> arrToSet(int[] nums) {
        int length = nums.length;
        Set<Integer> set = new HashSet<>();
        for(int i = 0; i < length; i++) {
            set.add(nums[i]);
        }
        return set;
    }
}